// src/router/utils/resetRouter.ts
import type { App } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import { routes } from './../routes/base'

// 创建临时路由实例用于重置
let router: ReturnType<typeof createRouter>

/**
 * 重置路由
 */
export function resetRouter() {
  // 创建新的空路由
  const newRouter = createRouter({
    history: createWebHistory(),
    routes: []
  })
  
  // 移除旧路由
  if (router) {
    router.getRoutes().forEach(route => {
      if (route.name) {
        router.removeRoute(route.name)
      }
    })
  }
  
  // 添加基础路由
  routes.forEach(route => {
    newRouter.addRoute(route)
  })
  
  router = newRouter
}

/**
 * 配置路由器
 */
export function setupRouter(app: App) {
  router = createRouter({
    history: createWebHistory(),
    routes
  })
  app.use(router)
}